/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2020/8/1 2:07 下午
 * @version V1.0
 */
import { pathToRegexp } from 'path-to-regexp';
import { message } from 'antd';
import { getChildCascader } from 'utils/index';
import {
    createTeacherUser,
    deleteTeacherUser,
    getDepartmentTreeList,
    getTeacherList,
    changeTeacherUserInfo,
} from 'api/index';

export default {
    namespace: 'teachers',
    state: {
        dataSource: {},
        keyword: '',
        reqPageInfo: {
            pageNumber: 1,
            pageSize: 10,
        },
        modalTitle: '新增教员',
        modalVisible: false,
        itemData: {},
        treeData: [],
    },
    reducers: {
        setDataSource(state, { payload: { dataSource, keyword, reqPageInfo } }) {
            return {
                ...state,
                dataSource,
                keyword,
                reqPageInfo,
            };
        },
        setModalVisible(state, { payload: modalVisible }) {
            return { ...state, modalVisible };
        },
        setModalTitle(state, { payload: modalTitle }) {
            return { ...state, modalTitle };
        },
        setItemData(state, { payload: itemData }) {
            return { ...state, itemData };
        },
        setUnitsTreeData(state, { payload: treeData }) {
            return { ...state, treeData };
        },
    },
    effects: {
        /**
         * @desc 教员列表
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getDataSource({ payload }, { call, put, select }) {
            const result = yield call(getTeacherList, payload);
            if (result.status === 'success') {
                const dataSource = result.result;
                yield put({
                    type: 'setDataSource',
                    payload: {
                        dataSource,
                        keyword: payload.keyword,
                        reqPageInfo: payload.reqPageInfo,
                    },
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 获取单位
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getUnitsTreeData({ payload }, { call, put, select }) {
            const result = yield call(getDepartmentTreeList, payload);
            if (result.status === 'success') {
                const data = result.result;
                const treeData = data.map(c => {
                    if (!c.hasLowerDepartment) {
                        return {
                            value: c.departmentId,
                            label: c.departmentName,
                        };
                    } else {
                        return {
                            value: c.departmentId,
                            label: c.departmentName,
                            children: getChildCascader(c.lowerDepartmentModel),
                        };
                    }
                });
                yield put({
                    type: 'setUnitsTreeData',
                    payload: treeData,
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 新增教员
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* addTeacher({ payload, callback }, { call, put, select }) {
            const result = yield call(createTeacherUser, payload);
            if (result.status === 'success') {
                message.success(`添加${payload.realName}成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 更改教员
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* updateTeacherUserInfo({ payload, callback }, { call, put, select }) {
            const result = yield call(changeTeacherUserInfo, payload);
            if (result.status === 'success') {
                message.success(`修改${payload.teacherUserName}成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 删除教员
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* removeTeacherUser({ payload, callback }, { call, put, select }) {
            const result = yield call(deleteTeacherUser, payload);
            if (result.status === 'success') {
                message.success(`删除成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname }) => {
                if (pathToRegexp('/users/teachers').exec(pathname)) {
                }
            });
        },
    },
};
